package com.leoman.resource.service.impl;

import com.leoman.category.dao.CategoryDao;
import com.leoman.category.entity.Category;
import com.leoman.common.service.impl.GenericManagerImpl;
import com.leoman.entity.Constant;
import com.leoman.resource.service.StillService;
import com.leoman.resources.dao.GameResourcesDao;
import com.leoman.resources.dao.MusicResourceDao;
import com.leoman.resources.dao.StillResourceDao;
import com.leoman.resources.entity.GameResource;
import com.leoman.resources.entity.MusicResource;
import com.leoman.resources.entity.StillResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 蔡琦 on 2016/6/28 0028.
 *
 */
@Service
public class StillServiceImpl extends GenericManagerImpl<StillResource, StillResourceDao> implements StillService {

    @Autowired
    private StillResourceDao stillResourceDao;

    @Autowired
    private MusicResourceDao musicResourceDao;

    @Autowired
    private GameResourcesDao gameResourcesDao;

    @Autowired
    private CategoryDao categoryDao;

    @Override
    public Page<StillResource> page(Integer pageNum, Integer pageSize) {
        return stillResourceDao.findAll(new PageRequest(pageNum - 1,pageSize, Sort.Direction.DESC,"id"));
    }

    @Override
    public List<Map<String, Object>> findListByType(Long categoryId) {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map;

        if (categoryId.equals(Constant.RESOURCE_TYPE_001)) {

            // 查询剧照资源
            List<Category> categoryList = categoryDao.findType(Constant.CATEGORY_TYPE_002);
            for (Category category : categoryList) {
                map = new HashMap<>();
                map.put("id", category.getId());
                map.put("name", category.getName());
                list.add(map);
            }

        }

        if (categoryId.equals(Constant.RESOURCE_TYPE_002)) {

            // 查询音乐资源
            List<MusicResource> musicResourceList = musicResourceDao.findAll();
            for (MusicResource musicResource : musicResourceList) {
                map = new HashMap<>();
                map.put("id", musicResource.getId());
                map.put("name", musicResource.getName());
                list.add(map);
            }
        }

        if (categoryId.equals(Constant.RESOURCE_TYPE_003)) {

            // 查询游戏资源
            List<GameResource> gameResourceList = gameResourcesDao.findAll();
            for (GameResource gameResource : gameResourceList) {
                map = new HashMap<>();
                map.put("id", gameResource.getId());
                map.put("name", gameResource.getName());
                list.add(map);
            }
        }

        return list;
    }

    @Override
    public void deleteStill(Long stillId) {
        StillResource stillResource = stillResourceDao.findOne(stillId);
        delete(stillResource);

        // 将对应分类的数量减1
        Category category = stillResource.getCategory();
        if (category != null) {
            category.setCount(category.getCount() - 1);
            categoryDao.save(category);
        }
    }

    @Override
    public List<StillResource> findByCategoryId(Long categoryId) {
        return stillResourceDao.iFindListByCategoryId(categoryId);
    }
}
